<template>
  <div class="container">
    <el-dialog title="新增学科" :visible="addShow" width="390px" :close-on-click-modal="false" :before-close="cancel">
      <el-form label-width="80px" :model="addForm" ref="addRef" :rules="myrules">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="addForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="addForm.enVisible"
            inactive-color="#ff4949"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add } from '@/api/hmmm/subjects.js'
export default {
  props: ['addShow', 'current'],
  data() {
    return {
      addForm: {
        // 是否可见
        enVisible: 1,
        subjectName: ''
      },
      // 表单校验
      myrules: {
        subjectName: [{ required: true, message: '请输入活动名称', trigger: 'blur' }, { trigger: 'change' }]
      }
    }
  },
  methods: {
    cancel() {
      this.$emit('update:addShow', false)
      this.$refs.addRef.resetFields()
      this.addForm.enVisible = 1
    },
    // 新增功能确定
    submit() {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        await add({
          subjectName: this.addForm.subjectName,
          isFrontDisplay: this.addForm.enVisible
        })
        this.$emit('update:addShow', false)
        this.$emit('update:current', 1)
        this.$emit('addCon')
        this.$message.success('新增成功')
        this.$refs.addRef.resetFields()
        this.addForm.enVisible = 1
      })
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-footer {
  display: block;
  text-align: right;
}
</style>
